package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.Upload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@WebServlet("/back/product/add")
public class ProductAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


//        resp.setContentType("text/html;charset=UTF-8");
        String[] results = Upload.getData(req).split("@@");
        ProductModel productModel = new ProductModel();
        productModel.setCuisineId(Integer.parseInt(results[0]));
        productModel.setName(results[1]);
        productModel.setImgHref(results[2]);
        productModel.setNormalPrice(Double.parseDouble(results[3]));
        productModel.setPrice(Double.parseDouble(results[4]));
        productModel.setContent(results[5]);
        productModel.setIsShow(Integer.parseInt(results[6]));
        productModel.setIsRecommend(Integer.parseInt(results[7]));

        IProductService iProductService = new ProductServiceImpl();
        int result = iProductService.add(productModel);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);
    }
}
